package com.example.file.dao;

import java.io.FileInputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class DataBase {

    static Connection conn = null;
    static HashMap<String, String> hashMap = new HashMap<>();
//驱动注册
    static {
//        配置读取
        String path = "./application.properties";
        System.out.println(path);
        try {
            FileInputStream fileOutputStream = new FileInputStream(path);
            byte[] bytes = fileOutputStream.readAllBytes();
            String s = new String(bytes);
            String[] split = s.split("\n");
            for (String value : split) {
                String[] split1 = value.split("==");
                hashMap.put(split1[0], split1[1]);
            }
            System.out.println("mysql路径:   " + hashMap.get("url"));
        } catch (Exception e) {
            System.out.println("格式错误或资源文件未找到");
        }
//注册驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("驱动注册成功...");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            conn = DriverManager.getConnection(hashMap.get("url"), hashMap.get("user"), hashMap.get("pwd"));
            System.out.println("数据库连接成功...");
        } catch (SQLException throwables) {
            System.out.println("数据库连接失败，请检查数据库权限");
        }

    }
//查询类型
    public static List<String> selectType() {
        List<String> names = new ArrayList<>();
        Statement statement = null;
        try {
            statement = conn.createStatement();
            ResultSet resultSet = statement.executeQuery("select * from type ");
            while (resultSet.next()) {
                String id = resultSet.getString(1);
                String name = resultSet.getString("name");
                names.add(name);
            }
            close(statement);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return names;
    }
//查找内容查询
    public static List<List<String>> selectMessage(List<String> type, String message) {
        int size = 0;
        if (type.size() == 0) {
            type = selectType();
            size = 1;
        } else {
            if (type.size() == 1) {
                size = 4;
            } else if (type.size() == 2 || type.size() == 3) {
                size = 2;
            } else if (type.size() > 4) {
                size = 1;
            }
        }
        List<List<String>> names = new ArrayList<>();
        Statement statement = null;
        try {
            statement = conn.createStatement();
            for (String s : type) {
                ResultSet resultSet = statement.executeQuery("select * from " + s + " where head like '%" + message + "%' limit " + size);
                while (resultSet.next()) {
                    String head = resultSet.getString("head");
                    String info = resultSet.getString("info");
                    List<String> map = new ArrayList<>();
                    map.add(s);
                    map.add(head);
                    map.add(info);
                    names.add(map);
                }
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        close(statement);
        return names;
    }

    //添加记错数据
    public static boolean insert(String head, String info, String type) {
        String sql = "insert into " + type + " (head, info)values ('" + head + "','" + info + "')";
        Statement stmt = null;
        try {
            stmt = conn.createStatement();
        } catch (SQLException throwables) {
            System.out.println("获取Statement出错。。。");

            throwables.printStackTrace();
            return false;
        }
        int i = 0;
        try {
            i = stmt.executeUpdate(sql);
            System.out.println(i);
//            i = stmt.executeUpdate(sql);
        } catch (SQLException throwables) {
            System.out.println("执行SQL出错。。。");
            throwables.printStackTrace();

        }
        close(stmt);
        return i > 0;
    }

    public static void close(Statement stmt) {
        try {
            stmt.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
